package com.zx.video28181.sip;

import android.javax.sip.InvalidArgumentException;
import android.javax.sip.ListeningPoint;
import android.javax.sip.ObjectInUseException;
import android.javax.sip.PeerUnavailableException;
import android.javax.sip.SipException;
import android.javax.sip.SipFactory;
import android.javax.sip.SipProvider;
import android.javax.sip.SipStack;
import android.javax.sip.TransportNotSupportedException;
import android.javax.sip.address.Address;
import android.javax.sip.address.AddressFactory;
import android.javax.sip.address.SipURI;
import android.javax.sip.header.CSeqHeader;
import android.javax.sip.header.CallIdHeader;
import android.javax.sip.header.ContactHeader;
import android.javax.sip.header.ExpiresHeader;
import android.javax.sip.header.FromHeader;
import android.javax.sip.header.HeaderFactory;
import android.javax.sip.header.MaxForwardsHeader;
import android.javax.sip.header.ToHeader;
import android.javax.sip.header.ViaHeader;
import android.javax.sip.message.MessageFactory;
import android.javax.sip.message.Request;

import com.zx.video28181.activity.GB28181Params;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.TooManyListenersException;

public class SipStackAndroid {


    private SipStack sipStack;

    private SipFactory sipFactory;

    private AddressFactory addressFactory;

    private HeaderFactory headerFactory;

    private MessageFactory messageFactory;

    private SipProvider sipProvider;

    /** Here we initialize the SIP stack. */
    @SuppressWarnings("deprecation")
    public SipStackAndroid(String ip, byte[] mPreviewBuffer, ClientListener.InitAboutCamera initAboutCamera) throws PeerUnavailableException,
            TransportNotSupportedException, InvalidArgumentException, ObjectInUseException, TooManyListenersException {
        sipFactory = SipFactory.getInstance();
        sipFactory.setPathName("android.gov.nist");
        Properties properties = new Properties();
        properties.setProperty("android.javax.sip.STACK_NAME", "cameraReg");
        properties.setProperty("android.javax.sip.IP_ADDRESS", ip);

        properties.setProperty("gov.nist.javax.sip.TRACE_LEVEL", "16");
        properties.setProperty("gov.nist.javax.sip.SERVER_LOG", "sip_server_log");
        properties.setProperty("gov.nist.javax.sip.DEBUG_LOG", "sip_debug_log");

        sipStack = sipFactory.createSipStack(properties);
        headerFactory = sipFactory.createHeaderFactory();
        addressFactory = sipFactory.createAddressFactory();
        messageFactory = sipFactory.createMessageFactory();

//        ListeningPoint tcp = sipStack.createListeningPoint(port, "tcp");
        ListeningPoint udp = sipStack.createListeningPoint("0.0.0.0",5099 ,GB28181Params.defaultProtol);

//        sipProvider = sipStack.createSipProvider(tcp);
//        sipProvider.addSipListener( this);
        sipProvider = sipStack.createSipProvider(udp);
        ClientListener listener =new ClientListener(addressFactory,headerFactory,messageFactory,sipProvider, mPreviewBuffer,initAboutCamera);
        sipProvider.addSipListener(listener);
    }

    /**
     * This method uses the SIP stack to send a message. 第一个参数：用户名 第二个参数：IP地址
     * 第三个参数：消息内容
     */
    public void sendMessage(int exs)
            throws ParseException, InvalidArgumentException, SipException {
        //from
        SipURI from = addressFactory.createSipURI(GB28181Params.getLocalSIPDeviceId(), GB28181Params.getLocalSIPIPAddress()+":"+GB28181Params.getLocalSIPPort());
        Address fromNameAddress = addressFactory.createAddress(from);
        FromHeader fromHeader = headerFactory.createFromHeader(fromNameAddress, "From-Register-");

       //to
        SipURI toAddress = addressFactory.createSipURI(GB28181Params.getLocalSIPDeviceId(), GB28181Params.getLocalSIPIPAddress()+":"+GB28181Params.getLocalSIPPort());
        Address toNameAddress = addressFactory.createAddress(toAddress);
        ToHeader toHeader = headerFactory.createToHeader(toNameAddress, null);

        SipURI requestURI = addressFactory.createSipURI(GB28181Params.getRemoteSIPServerID(), GB28181Params.getSIPServerIPAddress()+":"+GB28181Params.getRemoteSIPServerPort());
        requestURI.setTransportParam(GB28181Params.defaultProtol);
        //via
        ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
        ViaHeader viaHeader = headerFactory.createViaHeader(GB28181Params.getSIPServerIPAddress(), GB28181Params.getRemoteSIPServerPort(), GB28181Params.defaultProtol, "branch1");
        viaHeader.setRPort();
        viaHeaders.add(viaHeader);

        CallIdHeader callIdHeader = sipProvider.getNewCallId();
        @SuppressWarnings("deprecation")
        CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1, Request.REGISTER);
        MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);

        Request request = messageFactory.createRequest(requestURI, Request.REGISTER, callIdHeader, cSeqHeader,
                fromHeader, toHeader, viaHeaders, maxForwards);
        SipURI contactURI = addressFactory.createSipURI(GB28181Params.getLocalSIPDeviceId(), GB28181Params.getLocalSIPIPAddress()+":"+GB28181Params.getLocalSIPPort());
        contactURI.setPort(getPort());
        Address contactAddress = addressFactory.createAddress(contactURI);
        ContactHeader contactHeader = headerFactory.createContactHeader(contactAddress);
        ExpiresHeader expiresHeader = headerFactory.createExpiresHeader(exs);
        request.addHeader(expiresHeader);
        request.addHeader(contactHeader);
//        ContentTypeHeader contentTypeHeader = headerFactory.createContentTypeHeader("text", "plain");
//        request.setContent(message, contentTypeHeader);
        sipProvider.sendRequest(request);
    }



    @SuppressWarnings("deprecation")
    public String getHost() {
        String host = sipStack.getIPAddress();
        return host;
    }

    @SuppressWarnings("deprecation")
    public int getPort() {
        int port = sipProvider.getListeningPoint().getPort();
        return port;
    }



}

